<script setup lang="ts">
import { Edit } from '@element-plus/icons-vue'
import UploadAvatar from './uploadAvatar.vue'

const uploadAvatarRef = $ref<InstanceType<typeof UploadAvatar>>()

let userInfo = $ref(useUserStoreHook().user)

async function uploadAvatar() {
  if (!uploadAvatarRef)
    return
  await uploadAvatarRef.showDialog({
    imgUrl: userInfo?.avatar ?? '',
  })
  userInfo = useUserStoreHook().user
}
</script>

<template>
  <div class="base-info">
    <div class="avatar">
      <img :src="userInfo?.avatar" alt="">
      <div class="mask" @click="uploadAvatar">
        <el-icon>
          <Edit />
        </el-icon>
      </div>
    </div>
    <div class="user-info">
      <div
        class="item"
      >
        <div class="left">
          用户名
        </div>
        <div class="right">
          {{ userInfo?.username }}
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          所属部门
        </div>
        <div class="right">
          <el-tag v-for="(item) in userInfo?.deptList" :key="item.id">
            {{ item.name }}
          </el-tag>
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          所属角色
        </div>
        <div class="right">
          <el-tag v-for="(item) in userInfo?.roleList" :key="item.id">
            {{ item.name }}
          </el-tag>
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          所属岗位
        </div>
        <div class="right">
          <el-tag v-for="(item) in userInfo?.postList" :key="item.id">
            {{ item.name }}
          </el-tag>
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          最后登录地址
        </div>
        <div class="right">
          {{ userInfo?.ipCity }}
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          最后登录IP
        </div>
        <div class="right">
          {{ userInfo?.loginIp }}
        </div>
      </div>
      <div
        class="item"
      >
        <div class="left">
          最后登录时间
        </div>
        <div class="right">
          {{ userInfo?.loginDate }}
        </div>
      </div>
    </div>
    <UploadAvatar ref="uploadAvatarRef" />
  </div>
</template>

<style scoped>
.avatar {
  position: relative;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }
  .mask {
    position: absolute;
    opacity: 0;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    cursor: pointer;
    transition: all 0.3s;
    &:hover {
      opacity: 1;
    }
  }
}
.user-info {
  display: grid;
  gap: 15px;
  margin-top: 25px;
  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--sy-layout-border-color);
    font-size: 14px;
  }
}
</style>
